﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCDataCenter;

public partial class WebUserControl_ControlNganhHang : System.Web.UI.UserControl
{
    BCDataCenter.DataAccessLayer.DAL dt;
    MethodClassCommon common;
    string error = "";
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if ((Session["username"] == null) || (Session["username"]==""))
            {
                Response.Redirect(Globals.ApplicationPath + "Default.aspx");
            }
            else
            {
                //Label3.Visible = false;
                TextBox5.Text = "";
            }
        }
    }

    void grid_visible()
    {
        GridView1.DataSource = null;
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "chon")
        {
            TextBox5.Text = "";
            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            TxtMaLoaiHang.Text = grv.Cells[0].Text;
            txtMoTa.Text = grv.Cells[1].Text;
            TabContainer1.ActiveTabIndex = 0;
            grid_visible();
        }
    }
    void dataGrid()
    {
        dt = new BCDataCenter.DataAccessLayer.DAL();
        ds = dt.MyExecuteQuery(ref error, "SEARCH_INPRODCT", CommandType.StoredProcedure, new SqlParameter("@parameter", TextBox5.Text));
        if (ds.Tables[0].Rows.Count == 1)
        {
            TxtMaLoaiHang.Text = ds.Tables[0].Rows[0][0].ToString();
            txtMoTa.Text = ds.Tables[0].Rows[0][1].ToString();
            TabContainer1.ActiveTabIndex = 0;
        }
        else
        {
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
            //Label3.Visible = false;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        dataGrid();
    }
    protected void TextBox5_TextChanged(object sender, EventArgs e)
    {
        dataGrid();
    }

    protected void ImgNew_Click(object sender, ImageClickEventArgs e)
    {
        TxtMaLoaiHang.Text = "";
        txtMoTa.Text = "";
        //Label3.Visible = false;
    }
    protected void ImgInsert_Click(object sender, ImageClickEventArgs e)
    {
        bool kq = false;
        //Label3.Visible = true;
        dt = new BCDataCenter.DataAccessLayer.DAL();
        kq = dt.MyExecuteNonQuery("insertInprodct", CommandType.StoredProcedure, ref error, new SqlParameter("@productkey",TxtMaLoaiHang.Text.Trim()), new SqlParameter("@description", txtMoTa.Text));
        if (kq)
        {

            uscMsgBox1.AddMessage("Thêm dữ liệu mới thành công", uscMsgBox.enmMessageType.Success);
            TxtMaLoaiHang.Text = "";
            txtMoTa.Text = "";

        }
        else
        {
            uscMsgBox1.AddMessage("Trùng mã loại hàng", uscMsgBox.enmMessageType.Error);
        }

       
        TxtMaLoaiHang.Focus();
    }
    protected void ImgSave_Click(object sender, ImageClickEventArgs e)
    {
        //Label3.Visible = true;
        int kq = 1;
        common = new MethodClassCommon();
        kq = common.Update_Data("updateInprodct",CommandType.StoredProcedure,ref error,new SqlParameter("@productkey", TxtMaLoaiHang.Text.Trim()),new SqlParameter("@description", txtMoTa.Text));
        if (kq==0)
        {

            uscMsgBox1.AddMessage("Cập nhật dữ liệu thành công", uscMsgBox.enmMessageType.Success);
            TxtMaLoaiHang.Text = "";
            txtMoTa.Text = "";
        }
        else
        {
            uscMsgBox1.AddMessage("Mã loại hàng không tồn tại", uscMsgBox.enmMessageType.Error);

        }
       
        TxtMaLoaiHang.Focus();
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        dataGrid();
    }
}